package com.it.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.it.mapper.EmpMapper;
import com.it.pro.Emp;
import com.it.pro.PageBean;
import com.it.pro.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
public class EmpColltroller {
   @Autowired
    private EmpMapper empMapper;
   @GetMapping("/emps")
    public Result page(
           String name,
           Integer gender,
           @DateTimeFormat(
                   pattern = "yyyy-MM-dd"
           ) LocalDate begin,
           @DateTimeFormat(
                   pattern = "yyyy-MM-dd"
           )LocalDate end,
           Integer page,
           Integer pageSize){
       //设置分页的条件
       Page pageInfo = PageHelper.startPage(page,pageSize);
       //普通的查询
       empMapper.findByPage(name,gender,begin,end);
      return Result.success(new PageBean(pageInfo.getTotal(),pageInfo.getResult()));


   }
   @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
       //循环遍历集合
       for (Integer id : ids) {
           empMapper.delete(id);
       }
       return Result.success();
   }
   @PostMapping("/emps")
    public Result save(@RequestBody Emp emp){
       empMapper.save(emp);
       return Result.success();
   }
   @GetMapping("/emps/{id}")
    public Result get(@PathVariable Integer id){
       Emp emp = empMapper.get(id);
       return Result.success(emp);
   }
   @PutMapping("/emps")
    public Result update(@RequestBody Emp emp){
       empMapper.update(emp);
       return Result.success();
   }

}
